<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            width: 400px;
            height: 400px;
            margin: 0 auto;
        }

        #box>div {
            float: left;
            width: 100px;
            height: 100px;
            margin-right: 10px;
            background: #000;
        }
    </style>
</head>

<body>
    <button>点击一下所有div变成红色</button>
    <div id="box">
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
<script>
    window.onload = function () {
        var yDiv = document.getElementById("box").getElementsByTagName("div")
        var yBtn = document.getElementsByTagName("button")[0];
        yBtn.onclick = function () {
            for (var i = 0; i < yDiv.length; i++) {
                yDiv[i].style.background = "red"
            }
        }
    }
</script>

</html>